const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    avatarUrl: defaultAvatarUrl,  
    nickname: ''
  },
  onLoad(){
    this.loadUserInfo();
  },
  // 选择头像
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    this.setData({
      avatarUrl
    })
    // 存储到本地
    wx.setStorageSync('userAvatar', avatarUrl)
  },

  // 获取昵称
  getName(e) {
    const { value } = e.detail;
    this.setData({
      nickname: value
    })
    wx.setStorageSync('userNickname', value)
  },
  // 从本地读取头像和昵称
  loadUserInfo(){
    const saveAvatar=wx.getStorageSync('userAvatar')
    const saveNickname=wx.getStorageSync('userNickname')
    // 如果有则显示到页面
    if(saveAvatar){
      this.setData({
        avatarUrl:saveAvatar
      })
    }
    if(saveNickname){
      this.setData({
        nickname:saveNickname
      })
    }
  },
  toAddressPage(){
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },
  toHistoricalOrders(){
    wx.navigateTo({
      url: '/pages/historicalOrders/historicalOrders',
    })
  }
})